from django.db import models
from utils import choices
from account import models as models_account
import os
import uuid
from django.conf import settings


# Create your models here.
def ti_uploadto(instance, filename ):
    return  os.path.join( instance.book.ISBN+'/data/'+filename)

def cover_uploadto(instance,filename ):

    return  os.path.join(instance.ISBN,  uuid.uuid1().hex+ '.' +filename.split('.')[-1])

class Book(models.Model):

    name = models.CharField(
        max_length=100,
        verbose_name='书名'
    )
    shortname=models.CharField(
        max_length=50,
        verbose_name='书名简称',
        blank=True,
        null=True,
    )
    press = models.CharField(
        max_length=100,
        verbose_name='出版社',
    )

    university = models.CharField(
        max_length=100,
        verbose_name='编著大学',
        blank=True,
        null=True,
    )
    university_short=models.CharField(
        max_length=50,
        verbose_name='编著大学_简称',
        blank=True,
        null=True,
    )
    author = models.CharField(
        max_length=300,
        verbose_name='作者',
    )

    version = models.CharField(
        max_length=20,
        verbose_name='版本',
    )
    cover=models.ImageField(
        upload_to = cover_uploadto,
        verbose_name='封面',
        blank=True,
        null=True,
    )
    ISBN=models.CharField(
        max_length=50,
        verbose_name='ISBN号',
    )
    infos=models.TextField(
        verbose_name='所有信息',
        blank=True,
        null=True,
    )
    clicks=models.IntegerField(
        verbose_name='点击量',
        default=0,
        blank=True,
        null=True,
    )

    __original_cover = None

    def __init__(self, *args, **kwargs):
        super(Book, self).__init__(*args, **kwargs)
        self.__original_cover = self.cover

    def save(self, *args, **kwargs):

        super(Book, self).save(*args, **kwargs)
        if self.cover!=self.__original_cover:
            path=os.path.join(settings.OSS_DIR, 'book', str(self.__original_cover))
            if os.path.exists(path):
                os.remove( os.path.join(settings.OSS_DIR, 'book', path))
            str1=os.path.join(settings.OSS_DIR, 'book', self.ISBN )
            if not os.path.exists(str1 ):
                os.makedirs(str1)
            os.system('cp ' + self.cover.file.name + ' '+str1 )

    def __str__(self):
        return   str(self.id)+'-'+self.infos
class Chapter(models.Model):
    book = models.ForeignKey(
        Book,
        on_delete=models.CASCADE,
        related_name='all_chapter',
        verbose_name='课本'
    )

    title=models.CharField(
        max_length=100,
        verbose_name='目录标题',
    )

    index1 = models.IntegerField(
        verbose_name='章',
        default=-1,
    )

    index2 = models.IntegerField(
        verbose_name='节',
        default=-1,
    )
    index3 = models.IntegerField(
        verbose_name='小节',
        default=-1,
    )
    index4 = models.IntegerField(
        verbose_name='预留',
        default=-1,
    )
    layers= models.IntegerField(
        verbose_name='层数',
        blank=True,
        null=True,
    )

    ti_count = models.IntegerField(
        verbose_name='该小节题目总数',
        default= -1
    )

    def __str__(self):
        return   str(self.id)+'-'+self.title

class Ti(models.Model):
    question = models.ImageField(
        #upload_to = ti_uploadto,
        verbose_name='题目',
        blank=True,
        null=True,
    )

    answer = models.ImageField(
        #upload_to = 'ti/answer/',
        verbose_name='答案',
        blank=True,
        null=True,
    )
    book = models.ForeignKey(
        Book,
        on_delete=models.CASCADE,
        related_name='book_tis',
        verbose_name='课本'
    )
    ti_count=models.IntegerField(
        verbose_name='小题数',
        blank=True,
        null=True,
    )
    ti_number = models.IntegerField(
        verbose_name='第几题'
    )

    xiaoti_number = models.IntegerField(
        blank=True,
        null=True,
        verbose_name='第几小题',
    )
    is_title=models.BooleanField(
        default=False,
        verbose_name='是否题干',
    )
    chapter=models.ForeignKey(
        Chapter,
        on_delete=models.CASCADE,
        related_name='chapter_tis',
        blank=True,
        null=True,
    )
    qa=models.ImageField(
        #upload_to=ti_uploadto,
        verbose_name='题目和答案',
        blank=True,
        null=True,
        #storage=OverwriteStorage(),
    )
    def __str__(self):

        return str(self.chapter.index1) +'.' + str(self.chapter.index2) +\
               '-' + str(self.ti_number) +'.' +str(self.xiaoti_number) +'('+str(self.id)




